
import java.util.Scanner;

public class Class4{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m  = scanner.nextInt();
        int n = scanner.nextInt();
        int[][] arr = new int[m][n];
        for (int i=0;i<m;i++){
            for (int j=0;j<n;j++){
                arr[i][j] = scanner.nextInt();
            }
        }
        int[][] dp = new int[m][n];
        dp[0][0] = 0;
        for (int i=1;i<m;i++){
            if (arr[i][0]!=arr[i-1][0])
                dp[i][0] = dp[i-1][0]+2;
            else
                dp[i][0] = dp[i-1][0]+1;
        }
        for (int j=1;j<n;j++){
            if (arr[0][j]!=arr[0][j-1]){
                dp[0][j] = dp[0][j-1]+2;
            }else
                dp[0][j] = dp[0][j-1]+1;
        }
        for (int i=1;i<m;i++){
            for (int j=1;j<n;j++){
                int temp_row = dp[i-1][j];
                if (arr[i][j]==arr[i-1][j]){
                    temp_row++;
                }else {
                    temp_row +=2;
                }
                int temp_col = dp[i][j-1];
                if (arr[i][j]==arr[i][j-1]){
                    temp_col++;
                }else {
                    temp_col+=2;
                }
                dp[i][j] = Math.min(temp_row,temp_col);
            }
        }
        System.out.println(dp[m-1][n-1]);
    }
}
